<template>
  <div class="base-container" id="main">
    <div style="display: flex;font-weight: bold;color: #000000;justify-content: center;font-size: 29px;padding: 20px">
      地方标准项目建议表
    </div>
    <div
      style="display: flex;font-weight: bold;color: #000000;justify-content: center;font-size: 21px;margin-bottom: 10px;">
      （试行）
    </div>
    <t-form labelAlign="top" ref="form" :data="formData" :rules="rules" class="step-form table-form"
            labelWidth="140px">
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>项目名称</t-col>
        <t-col :span="10">
          <t-form-item name="projectName">
            <t-input clearable v-model="formData.projectName"></t-input>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>标准性质</t-col>
        <t-col :span="4">
          <t-form-item name="standardProperty">
            <t-radio-group clearable v-model="formData.standardProperty"
                           :options="dict.type.standard_property">
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>标准体系</t-col>
      <t-col :span="4">
         <t-tree-select clearable filterable  :treeProps="{ keys: { value: 'id' } }"
                    v-model="formData.systemId" :data="standardSystemOptions"
                    placeholder="选择标准体系"></t-tree-select>
      </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>制定/修订</t-col>
        <t-col :span="4">
          <t-form-item name="formulateType">
            <t-radio-group clearable v-model="formData.formulateType"
                           :options="dict.type.formulate_type">
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require"
                                             v-if="formData.formulateType && formData.formulateType!=='1'">*</span>被修订标准号
        </t-col>
        <t-col :span="4">
          <t-form-item v-if="formData.formulateType && formData.formulateType!=='1'" name="formulateType">
            <t-input clearable v-model="formData.reviseNum"></t-input>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>技术归口单位</t-col>
        <t-col :span="4">
          标准委员会
        </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>主管部门</t-col>
        <t-col :span="4">
          <t-tree-select clearable filterable :treeProps="{ keys: { value: 'id' } }"
                         v-model="formData.manageDeptId" :data="deptMenuOptions" placeholder="选择主管部门"></t-tree-select>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>起草单位</t-col>
        <t-col :span="10">
          <standard-company ref="standard-company" tableSize="small" v-model="formData.draftUnits"
                            :editOpen="true"></standard-company>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>联系人</t-col>
        <t-col :span="4">
          <t-form-item name="linkMan">
            <t-input clearable v-model="formData.linkMan"></t-input>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>联系方式</t-col>
        <t-col :span="4">
          <t-form-item name="linkWay">
            <t-input placeholder="请输入手机号码" clearable v-model="formData.linkWay"></t-input>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>计划完成时间</t-col>
        <t-col :span="4">
          <t-form-item name="completeDate">
            <t-date-picker style="width: 390px" clearable v-model="formData.completeDate"></t-date-picker>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>建议表申请时间</t-col>
        <t-col :span="4">
          <t-form-item name="jybApplyDate">
            <t-date-picker style="width: 390px" clearable type="date" v-model="formData.jybApplyDate"></t-date-picker>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>目的、意义（标准制修订的背景、必要性和可行性简要论述）</t-col>
        <t-col :span="10">
          <t-form-item name="aim">
          
            <tiny-mce-editor key="aim"
                             v-model="formData.aim"></tiny-mce-editor>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>采标情况</t-col>
        <t-col :span="10" style="display:block;">
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require">*</span>（1）采用国际标准：</t-col>
            <t-col :span="10">
              <t-form-item name="bidCondition.bidType">
                <t-radio-group clearable v-model="formData.bidCondition.bidType"
                               :options="dict.type.bid_type">
                </t-radio-group>
              </t-form-item>
            </t-col>
          </t-row>
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require" v-if="formData.bidCondition.bidType!=='1'">*</span>（2）采用程度：
            </t-col>
            <t-col :span="10">
              <t-form-item v-if="formData.bidCondition.bidType!=='1'" name="bidCondition.bidDegree">
                <t-radio-group clearable v-model="formData.bidCondition.bidDegree"
                               :options="dict.type.bid_degree">
                </t-radio-group>
              </t-form-item>
            </t-col>
          </t-row>
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require" v-if="formData.bidCondition.bidType!=='1'">*</span>（3）采标名称：</t-col>
            <t-col :span="10">
              <t-form-item v-if="formData.bidCondition.bidType!=='1'" name="bidCondition.bidName">
                <t-input clearable v-model="formData.bidCondition.bidName"></t-input>
              </t-form-item>
            </t-col>
          </t-row>
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require" v-if="formData.bidCondition.bidType!=='1'">*</span>（4）采 标 号：</t-col>
            <t-col :span="10">
              <t-form-item v-if="formData.bidCondition.bidType!=='1'" name="bidCondition.bidNum">
                <t-input clearable v-model="formData.bidCondition.bidNum"></t-input>
              </t-form-item>
            </t-col>
          </t-row>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>范围和主要技术内容</t-col>
        <t-col :span="10">
          <t-form-item name="radius">
          
            <tiny-mce-editor key="radius"
                             v-model="formData.radius"></tiny-mce-editor>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>国内外情况简要说明</t-col>
        <t-col :span="10">
          <t-form-item name="description">
          
            <tiny-mce-editor key="description"
                             v-model="formData.description"></tiny-mce-editor>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>有关法律法规和强制性标准的关系</t-col>
        <t-col :span="10">
          <t-form-item name="relation">
          
            <tiny-mce-editor key="relation"
                             v-model="formData.relation"></tiny-mce-editor>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>是否有科研项目支撑</t-col>
        <t-col :span="2">
          <t-form-item name="projectUp">
            <t-radio-group clearable v-model="formData.projectUp"
                           :options="dict.type.yes_or_not">
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require"
                                             v-if="formData.projectUp && formData.projectUp==='1'">*</span>科研项目编号及名称
        </t-col>
        <t-col :span="6">
          <t-form-item v-if="formData.projectUp && formData.projectUp==='1'" name="projectUpInfos"
                       :show-error-message="false">
            <suggest-project ref="suggest-project" :edit-open="true" table-size="small"
                             v-model="formData.projectUpInfos"></suggest-project>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>是否涉及专利</t-col>
        <t-col :span="2">
          <t-form-item name="patentUp">
            <t-radio-group clearable v-model="formData.patentUp"
                           :options="dict.type.yes_or_not">
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require"
                                             v-if="formData.patentUp && formData.patentUp==='1'">*</span>专利号及名称
        </t-col>
        <t-col :span="6">
          <t-form-item v-if="formData.patentUp && formData.patentUp==='1'" name="patentUpInfos"
                       :show-error-message="false">
            <suggest-patent ref="suggest-patent" :edit-open="true" table-size="small"
                            v-model="formData.patentUpInfos"></suggest-patent>
          </t-form-item>
        </t-col>
      </t-row>
    </t-form>

    <t-row style="margin-top: 20px;text-align: center">
      <t-col :span="12">
        <t-space :size="10">
          <t-button theme="primary" :loading="applyLoading" @click="onSubmit(1)">{{
              applyLoading ? '提交中...' : '暂存'
            }}
          </t-button>
          <t-button theme="primary" :loading="applyLoading" @click="onSubmit(2)">{{
              applyLoading ? '提交中...' : '提交'
            }}
          </t-button>
        </t-space>
      </t-col>
    </t-row>
  </div>
</template>

<script>
import standardCompany from "@/pages/bzh/standard/components/standard-company.vue";
import TinyMceEditor from "@/components/TinyMceEditor/index.vue";
import suggestPatent from "./components/suggest-patent.vue";
import suggestProject from "./components/suggest-project.vue";


export default {
  name: "suggest-apply",
  dicts: ['standard_property', 'formulate_type', 'bid_type', 'bid_degree', 'yes_or_not'],
  components: {
    standardCompany,
    TinyMceEditor,
    suggestPatent,
    suggestProject
  },
  data() {
    return {
      deptMenuOptions: [],
      formData: {
        projectName: '',
        completeDate: '',
        standardProperty: '1',
        formulateType: '1',
        reviseNum: '',
        manageDeptId: 100,
        linkMan: '',
        linkWay: '',
        draftUnits: [],
        aim: '',
        bidCondition: {
          bidType: '1',
          bidDegree: '',
          bidName: '',
          bidNum: '',
        },
        radius: '',
        description: '',
        relation: '',
        projectUp: '0',
        patentUp: '0',
        projectUpInfos: [],
        patentUpInfos: [],
        systemId:'',
        jybApplyDate: new Date(),
      },
      standardSystemOptions:[],
      rules: {
        projectName: [
          {
            required: true,
            message: "项目名称不能为空",
          }
        ],
        standardProperty: [
          {
            required: true,
            message: "标准性质不能为空",
          }
        ],
        formulateType: [
          {
            required: true,
            message: "制定/修订不能为空",
          }
        ],
        completeDate: [
          {
            required: true,
            message: "计划完成时间不能为空",
          }
        ],
        reviseNum: [
          {
            required: true,
            message: "被修订标准号不能为空",
          }
        ],
        systemId:[
          {
            required: true,
            message: "标准体系不能为空",
          }
        ],
        manageDeptId: [
          {
            required: true,
            message: "主管部门不能为空",
          }
        ],
        linkMan: [
          {required: true, message: "联系人姓名不能为空"}
        ],
        linkWay: [
          {
            required: true,
            message: "联系人电话不能为空",
          },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "联系人电话请输入正确的手机号码",
          },
        ],
        aim: [
          {
            required: true,
            message: "目的意义不能为空",
          }
        ],
        draftUnits: [{required: true, validator: (val) => val && val.length, message: "起草单位不能为空", type: "error"}],
        'bidCondition.bidType': [
          {
            required: true,
            message: "采标情况-采用国际标准不能为空",
          }
        ],
        'bidCondition.bidDegree': [
          {
            required: true,
            message: "采标情况-采用程度不能为空",
          }
        ],
        'bidCondition.bidName': [
          {
            required: true,
            message: "采标情况-采标名称不能为空",
          }
        ],
        'bidCondition.bidNum': [
          {
            required: true,
            message: "采标情况-采标号不能为空",
          }
        ],
        radius: [
          {
            required: true,
            message: "范围和主要技术内容不能为空",
          }
        ],
        description: [
          {
            required: true,
            message: "国内外情况简要说明不能为空",
          }
        ],
        relation: [
          {
            required: true,
            message: "有关法律法规和强制性标准的关系不能为空",
          }
        ],
        projectUp: [
          {
            required: true,
            message: "是否有科研项目支撑不能为空",
          }
        ],
        projectUpInfos: [{
          required: true,
          validator: (val) => val && val.length,
          message: "科研项目编号及名称不能为空",
          type: "error"
        }],
        parentUp: [{required: true, message: "是否涉及专利不能为空",}],
        parentUpInfos: [{required: true, validator: (val) => val && val.length, message: "专利号及名称不能为空", type: "error"}],
      },
      applyLoading: false,
    }
  },
  created() {
    this.getDeptTreeSelect();
    this.getStandardSystemTree()
    if (this.$route.params.taskId) {
      this.$api.bzh.standard.getSuggest(this.$route.params.taskId).then(response => {
        if (!response.data) {

          return false;
        }
        this.formData = { ...this.formData, ...response.data}
        if (!this.formData.bidCondition) {
          this.formData.bidCondition = {}
        }
      }).catch(e => {
        this.$message.error(e.toString())
      })
    }
  },
  methods: {
    getStandardSystemTree() {
      return this.$api.bzh.standard.standardSystemTree().then(response => {
        this.standardSystemOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getDeptTreeSelect() {
      return this.$api.system.dept.treeSelectAllIn().then(response => {
        this.deptMenuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onSubmit(type) {
      if (type === 1) {
        this.$refs.form.validate({
          fields: ['projectName']
        }).then((validateResult) => {
          if (validateResult === true) {
            this.applyLoading = true;
            if (!this.formData.id) {
              this.$api.bzh.standard.addSuggest(this.formData).then(response => {
                this.formData.id = response.data.id;
                this.applyLoading = false;
                this.$message.success('暂存成功');
              }).catch((e) => {
                this.applyLoading = false;
                this.$message.error(e.toString());
              });
            } else {
              this.$api.bzh.standard.updateSuggest(this.formData).then(response => {
                this.applyLoading = false;
                this.$message.success('暂存成功');
              }).catch((e) => {
                this.applyLoading = false;
                this.$message.error(e.toString());
              });
            }
          } else {
            const firstError = Object.values(validateResult)[0]?.[0]?.message;
            this.$message.error(firstError);
          }
        })

      } else {

        this.$refs.form.validate().then((validateResult) => {
          if (validateResult === true) {
            const isValidate = this.$refs["standard-company"].checkData()
            if (!isValidate) {
              return false
            }

            if (this.formData.projectUp === '1') {
              const isValidate2 = this.$refs["suggest-project"].checkData()
              if (!isValidate2) {
                return false
              }
            }
            if (this.formData.patentUp === '1') {
              const isValidate3 = this.$refs["suggest-patent"].checkData()
              if (!isValidate3) {
                return false
              }
            }
            const confirmDia = this.$dialog.confirm({
              header: "提交后不可再编辑，会自动进入到下一阶段",
              body: `是否确认提交？`,
              theme: "warning",
              onConfirm: ({e}) => {
                confirmDia.update({
                  confirmBtn: {
                    loading: true,
                  },
                })
                this.applyLoading = true;
                this.$api.bzh.standard.submitSuggest(this.formData).then(response => {
                  this.applyLoading = false;
                  confirmDia.destroy();
                  this.$message.success('提交成功');
                  this.close();
                }).catch((e) => {
                  this.applyLoading = false;
                  confirmDia.update({
                    confirmBtn: {
                      loading: false,
                    },
                  })
                  this.$message.error(e.toString());
                });
              },
            })


          } else {
            const firstError = Object.values(validateResult)[0]?.[0]?.message;
            this.$message.error(firstError);
          }
        });
      }
    },
    close() {
      this.$tab.closeOpenPage({path: "/bzh/suggest/suggest-my"});
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .t-table__empty {
  min-height: initial;
}
</style>
